//
//  FriendsDb.mm
//  iLo
//
//  Created by Huahang Liu on 10/15/09.
//  Copyright 2009 University of Southern California. All rights reserved.
//

#import "FriendsDb.h"


@implementation FriendsDb

@synthesize friends;

- (id)init {
    friends = [[NSMutableArray alloc] init];
    return [super init];
}

- (void)refreshFriendsFromURL:(NSURL*)url {
    
    NSMutableURLRequest* theRequest = [NSMutableURLRequest requestWithURL:url 
                                                              cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                          timeoutInterval:10.0f];
    [theRequest setHTTPMethod:@"GET"];    
    NSURLResponse* theResponse = NULL;
    NSError* theError = NULL;
    NSData* theResponseData = [NSURLConnection sendSynchronousRequest:theRequest 
                                                    returningResponse:&theResponse 
                                                                error:&theError];
    NSString* theResponseString = [[[NSString alloc] initWithData:theResponseData 
                                                         encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@", theResponseString);
    NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:theResponseData] autorelease];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    
    NSLog(@"didStartElement: %@", elementName);
    if([elementName compare:@"Locations"] == NSOrderedSame)
    {
        state = E_Locations;
        friends_ = [[NSMutableArray alloc] init];
    }
    else if([elementName compare:@"Location"] == NSOrderedSame)
    {
        friend_ = [[Friend alloc] init];
        state = E_Location;
    }
    else if([elementName compare:@"UserName"] == NSOrderedSame)
    {
        state = E_UserName;
    } 
    else {}
    
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName {
    
    NSLog(@"didEndElement: %@", elementName);
    if([elementName compare:@"Locations"] == NSOrderedSame)
    {
        state = E_Locations;
        [friends release];
        friends = friends_;
        friends_ = nil;
        //[self.tableView reloadData];
    }
    else if([elementName compare:@"Location"] == NSOrderedSame)
    {
        [friends_ addObject:friend_];
        [friend_ release]; friend_ = nil;
        state = E_Locations;
    }  
    else if([elementName compare:@"UserName"] == NSOrderedSame)
    {
        state = E_Location;
    } 
    else {}
    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    
    NSLog(@"foundCharacters: %@", string);
    switch (state) {
        case E_UserName:
        {
            NSString* userName = [NSString stringWithFormat:@"%@", string];
            friend_.name = userName;
            break;
        }
        default:
            break;
    }
    
}

- (void)dealloc {
    [friends  release];
    [friend_  release];
    [friends_ release];
    [super dealloc];
}

@end
